﻿$(function () {

    Sys.WebForms.PageRequestManager.getInstance().add_initializeRequest(cancelPostBack);
    Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(OnBeginRequest);
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(OnEndRequest);

});

function cancelPostBack(sender, args) {

    if (Sys.WebForms.PageRequestManager.getInstance().get_isInAsyncPostBack())        
    {
        //alert('One postback at a time please');
        args.set_cancel(true);
    }
}  

function OnBeginRequest(sender, args) {
    //elem_ajax = sender._activeElement.id;
    //alert(elem_ajax.id );

    open_ajax_wait(sender);

}

var ajax_active_element;
function OnEndRequest(sender, args) {
    //elem_ajax = sender._activeElement.id;

    ajax_active_element = sender._activeElement;
    //alert(sender._updatePanelClientIDs);

    close_ajax_wait(sender);

    // Check to see if there's an error on this request.
    if (args.get_error() != undefined) {
        // If there is, show the custom error.
        //$get('Error').style.visibility = "visible";
        if (args.get_error().httpStatusCode != 0) {
            alert(args.get_error())
        }
        // Let the framework know that the error is handled, 
        //  so it doesn't throw the JavaScript alert.
        args.set_errorHandled(true);
    }


}

var wait_ajax_open = 0;

function open_ajax_wait(sender) {

    PanelUpdateID = '';
    if (sender != null) {
        if (sender._postBackSettings.panelsToUpdate != null) {
            if (sender._postBackSettings.panelsToUpdate[0].length > 0) {
                p = sender._postBackSettings.panelsToUpdate[0].split('$');
                PanelUpdateID = p[p.length - 1];
            }
        }
    }

    content_wait = $("#content_" + PanelUpdateID + "");

    //alert(PanelUpdateID);


    if (content_wait.length > 0) {
        mess = '<div style="font-family:Verdana;font-size:14px;"><img src="/style/img/busy.gif" /></div>';
        width_box = '100';
        top_box = (content_wait.height() - width_box - 30) / 2 + 'px';
        left_box = (content_wait.width() - width_box - 30) / 2 + 'px';

    } else {
        mess = $('#wait_update_box');
        if (mess.length == 0) {
            mess = '<div style="font-family:Verdana;font-size:14px;"><img src="/style/img/busy.gif" /><br> Wait a moment...</div>';
        }
        width_box = '200';
        top_box= ($(top).height() - width_box -30) / 2 + 'px';
        left_box = ($(top).width() - width_box -30) / 2 + 'px';
    }


    //    centerX: true,
    //   centerY: true,
    //            textAlign: 'center',

    wait_options = {
        message: mess,
        showOverlay: true,
        applyPlatformOpacityRules: true,
        fadeIn: 0,
        fadeOut: 0,
        overlayCSS: {
            backgroundColor: '#ffffff',
            opacity: 0,
            cursor: 'auto'
        },
        css: {
            border: '2px solid #FFD203',
            padding: '15px',
            backgroundColor: '#ffffff',
            '-webkit-border-radius': '10px',
            '-moz-border-radius': '10px',
            'border-radius': '10px',
            opacity: 1,
            color: '#444444',
            width: width_box,
            cursor: 'auto',
            top: top_box,
            left: left_box,
        }

    };


//wait_options = {
//    message: mess
//};

//  alert(PanelUpdateID);

if (PanelUpdateID == '') {
    open_ajax_block(wait_options);
} else {
    if (content_wait.length > 0) {
        content_wait.block(wait_options);
    } else {
        open_ajax_block(wait_options);
    }
}

}

function close_ajax_wait(sender) {


    if (sender == null) {
        close_ajax_block();
    } 
    
    else {

        content_wait = $("#content_" + sender._updatePanelClientIDs + "")
        if (content_wait.length > 0) {
            content_wait.unblock({ message: $('#dialog-modal') });
        } else {
            close_ajax_block();
        }
    }
    //$("body").css('cursor', 'crosshair');

}

function open_ajax_block(wait_options) {


    top.$.blockUI(wait_options);
    wait_ajax_open += 1;
    //alert('apri:' + wait_ajax_open);

}

function close_ajax_block() {

    wait_ajax_open -= 1;
    //alert('chiudi:' + wait_ajax_open);
    if (wait_ajax_open <= 0) {
        wait_ajax_open = 0;
        top.$.unblockUI({ message: $('#dialog-modal') });

        //alert('222');
        //$("body").css('cursor', 'crosshair');


    }


}



function on_update_panel_server_once(f) {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function () {
        f.call();
        Sys.WebForms.PageRequestManager.getInstance().remove_endRequest(arguments.callee);
    });

}
function on_update_panel_server(f) {
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function () {
        f.call();
    });

}

